package com.andlabs.gd.rendering;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;

import com.andlabs.gd.base.RenderingListener;
import com.andlabs.gd.objects.Hero;
import com.andlabs.gd.simulation.Simulation;

/**
 * @author johannes
 */
public class Renderer implements RenderingListener{
	private Context mContext;
	private Simulation mSimulation;
	private boolean mIsLoading = true;
	private float mZoomLevel;
	
	public Renderer(Context context, Simulation simulation){
		mContext = context;
		mSimulation = simulation;
		mZoomLevel = 1;
	}

	@Override
	public void initializeRendering() {
		Log.d("Test", "initializing rendering");
		
		do{
			initilaizeHeroRendering();
		}while(mSimulation.mHero == null);
			
		do{
			initializeMapRendering();
		}while(mSimulation.mMap == null);

		
		mIsLoading = false;
	}

	@Override
	public void initializeSplash() {
		Log.d("Test", "initializing splash");	
	}

	@Override
	public boolean isInitializingRendering() {
		return mIsLoading;
	}

	@Override
	public void render(Canvas canvas) {
		canvas.drawColor(Color.argb(255, 111, 79, 51));

    	renderMap(canvas);
    	renderHero(canvas);
//    	Paint paint = new Paint();
//    	paint.setColor(Color.WHITE);
//    
//    	canvas.drawLine(100, 100, 200, 100, paint);
//    	canvas.drawLine(200, 100, 200, 200, paint);
//    	
//    	canvas.drawLine(100, 100, 100, 200, paint);
//    	canvas.drawLine(200, 200, 100, 200, paint);

	}

	@Override
	public void renderSplash(Canvas canvas) {
		Log.d("Test", "rendering splash");	
		
		Paint paint = new Paint();
		paint.setColor(Color.WHITE);
		canvas.drawText("Loading...", 10, 10, paint);
	}
	
	private void initializeMapRendering(){
		if(mSimulation.mMap != null){
			mSimulation.mMap.initializeRendering(mContext);
		}
	}
	
	private void initilaizeHeroRendering(){
		if(mSimulation.mHero != null){
			mSimulation.mHero.initializeRendering(mContext);
		}
	}
	
	private void renderHero(Canvas canvas){
		synchronized(mSimulation.mHero){
			mSimulation.mHero.render(canvas, mZoomLevel);
		}
	}
	
	private void renderMap(Canvas canvas){
		mSimulation.mMap.render(canvas, mSimulation.mHero.mPosition);
	}
}

